package com.example.blogdemo

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast
import androidx.core.os.bundleOf

class MainActivity : AppCompatActivity() {
    val arrays = mutableListOf<ClickItem>()

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var listMain = findViewById<ListView>(R.id.listMan)
        initArray()
        listMain.setOnItemClickListener { adapterView, view, i, l ->
            arrays[i].click()
        }
        listMain.adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, arrays)
    }

    private fun initArray() {
        arrays.add(ClickItem("按钮1") {
            // 点击事件
           openActivity(ShowFragmentActivity::class.java,getSowFragment(BlankFragment::class.java,"默认的空界面"));
        })
        arrays.add(ClickItem("按钮2") {
            // 点击事件
        })
        arrays.add(ClickItem("按钮2") {
            // 点击事件
        })
    }
    private fun openActivity(java: Class<*>,bundle: Bundle= bundleOf()) {
        val intent= Intent(this,java)
        intent.putExtras(bundle)
        startActivity(intent)
    }

    private fun openFragment(fragment:Class<*>, title: String = "莫得名字"){
        openActivity(ShowFragmentActivity::class.java, getSowFragment(fragment,title))
    }

    fun showToast(toast: String) {
        Toast.makeText(this,toast,Toast.LENGTH_LONG).show()
    }
}


fun String.showToast(context: Context){
    Toast.makeText(context,this,Toast.LENGTH_LONG).show()
}